<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Product extends Backdata_controller{
	var $rule_post=array(
		array(
			'field'=>'title',
			'label'=>'产品名称',
			'rules'=>'required|max_lenx[255]'),
		array(
			'field'=>'type_id',
	        'rules'=>'intval'),	
		array(
			'field'=>'show',
			'rules'=>'intval'),
		array(
			'field'=>'recommend',
			'rules'=>'intval'),
		array(
			'field'=>'timeline',
			'rules'=>'trim'),
		array(
			'field'=>'field1',
			'rules'=>'trim'),
		array(
			'field'=>'field2',
			'rules'=>'trim'),
		array(
			'field'=>'field3',
			'rules'=>'trim'),
		array(
			'field'=>'field4',
			'rules'=>'trim'),
		array(
			'field'=>'field5',
			'rules'=>'trim'),
		array(
			'field'=>'field6',
			'rules'=>'trim'),
		array(
			'field'=>'field7',
			'rules'=>'trim'),
		array(
			'field'=>'field8',
			'rules'=>'trim'),
		array(
			'field'=>'field9',
			'rules'=>'trim'),
		array(
			'field'=>'field10',
			'rules'=>'trim'),
		array(
			'field'=>'field11',
			'rules'=>'trim'),
		array(
			'field'=>'field12',
			'rules'=>'trim'),
		array(
			'field'=>'content',
			'label'=>'详细内容',
			'rules'=>'required'),
		);
	
	var $search_post=array(
		array(
			'field'=>'title',
			'label'=>'产品名称',
			'rules'=>'trim'),
		array(
			'field'=>'timeline',
			'rules'=>'trim'),
		array(
			'field'=>'expire',
			'rules'=>'trim'),
	);	
		
	function on_load(){
		$this->load->model('dataset_atom','my_model');
		$this->load->library('category');
		$this->view=$this->table=strtolower(get_class($this));
		$this->category->table_name('admin_act');
		$this->category->block(strtolower(get_class($this)));
		$this->category->type_id($this->uri->segment(3,-1));
		$type_tree=$this->category->type_tree();
		$this->load->vars(array('type_tree'=>$type_tree));
	}

	function on_post(){
		return array('rule'=>$this->rule_post);
	}
	
	function on_put(){
		return array('rule'=>$this->rule_post);
	}
	
	function searchproduct(){
		$type_id=$this->uri->segment(3,-1);
		$this->backview->view("ajax/{$this->view}_searchproduct",get_defined_vars());
	}
	
	function searchproductpost(){
		$type_id=$this->uri->segment(3,-1);
		$this->load->database();
		$this->load->library('form_validation',$this->search_post);
		if(!$this->form_validation->run()){
			return $this->index();
		}
		$form=$this->form_validation->to_array();
		$form=$this->uri->encode(serialize($form));
		$type_id=$this->uri->segment(3,-1);
		redirect("{$this->title}/index/$type_id/1/null/10/$form");

	}
	function on_insert(&$form){
		if(empty($form['photo']))return;
		$photo=$this->_url_to_path($form['photo']);
		if(!file_exists($photo))return;
		$this->watermark2($photo);
	}
	
	function on_update(&$form){
		if(empty($form['photo']))return;
		$photo=$this->_url_to_path($form['photo']);
		if(!file_exists($photo))return;
		$this->watermark2($photo);
	}
	
	function watermark2($filename){
		//echo $filename;exit;
		$this->load->library('image_lib');
		$config['image_library'] = 'gd2';//(必须)设置图像库
		//$config['source_image'] =dirname(dirname(dirname(dirname(__FILE__))))."/..".$filename;//(必须)设置原图像的名字和路径. 路径必须是相对或绝对路径，但不能是URL.
		$config['source_image'] =$filename;
		//echo $config['source_image'];exit;
		$config['dynamic_output'] = FALSE;//TRUE 动态的存在(直接向浏览器中以输出图像),FALSE 写入硬盘
		$config['quality'] = '100%';//设置图像的品质。品质越高，图像文件越大
		//$config['new_image'] = dirname(dirname(dirname(dirname(__FILE__)))).$filename;//设置图像的目标名/路径。
			
		$config['wm_type'] = 'overlay';//(必须)设置想要使用的水印处理类型(text, overlay)
		//$config['wm_padding'] = '5';//图像相对位置(单位像素)
		$config['wm_vrt_alignment'] = 'middle';//竖轴位置 top, middle, bottom
		$config['wm_hor_alignment'] = 'center';//横轴位置 left, center, right
		$config['wm_vrt_offset'] = '0';//指定一个垂直偏移量（以像素为单位）
		$config['wm_hor_offset'] = '0';//指定一个横向偏移量（以像素为单位）
		/* 文字水印参数设置 */
		/*
			 $config['wm_text'] = 'Copyright 2008 - John Doe';//(必须)水印的文字内容
			$config['wm_font_path'] = 'ptj_system/fonts/type-ra.ttf';//字体名字和路径
			$config['wm_font_size'] = '16';//(必须)文字大小
			$config['wm_font_color'] = 'FF0000';//(必须)文字颜色，十六进制数
			$config['wm_shadow_color'] = 'FF0000';//投影颜色，十六进制数
		$config['wm_shadow_distance'] = '3';//字体和投影距离（单位像素）。
		*/
			
		/* 图像水印参数设置 */
		$config['wm_overlay_path'] = dirname(dirname(dirname(dirname(__FILE__)))).'/shuiyin.png';//水印图像的名字和路径
		$config['wm_opacity'] = '100';//水印图像的透明度
		$config['wm_x_transp'] = '4';//水印图像通道
		$config['wm_y_transp'] = '4';//水印图像通道
		$this->image_lib->initialize($config);
		$this->image_lib->watermark();
	}
	
	function _url_to_path($url){
		$upurl=rtrim(config_item('upload.url'),'/\\').'/';
		$updir=rtrim(config_item('upload.dir'),'/\\').'/';
		if(empty($url)||is_int($url)){
			return '';
		}
		if(!strncasecmp($url,$upurl,strlen($upurl))){
			$url=$updir.substr($url,strlen($upurl));
		}
		return $url;
	}
}